#!/bin/bash
###################################################################### 
#Copyright (C) 2019  Kris Occhipinti
#https://filmsbykris.com

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation version 3 of the License

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
###################################################################### 

#find product title, price, url on Amazon based on UPC

url="https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords="

while [ 1 ]
do
  echo -n "Scan UPC: ";
  read upc;

  if [ "$upc" = "" ];then echo "Goodbye...";exit;fi

  results="$(wget "$url$upc" -qO-)";

  #Get Product Title
  product="$(echo "$results"|grep "s-access-detail-page"|sed "s/title=/\ntitle=/g"|grep '^title='|head -n 10|cut -d\" -f2)"

  if [ "$product" != "" ]
  then

    #output Product Title
    echo "$product"
    #output Price
    echo "$results"|grep 'sx-price-large' -A 3|tail -n 3|sed -e 's/<[^>]*>//g'|sed ':a;N;$!ba;s/\n/./g;s/ //g;s/$./$/' 
    #output Product URL
    echo "$results"|grep "s-access-detail-page"|sed "s/href=/\nhref=/g"|grep '^href='|head -n1|cut -d\" -f2
  else
    echo "Product Not Found on Amazon"
  fi
done